package step_2.checkers_board;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import step_2.players.Player;
import step_2.players.random.RandomPlayer;


public class Game  {

	static Checkers checkers = null;
	List<PlayerMove> moveHistory;
	static Board board;
	JFrame window;

	public Game() {
		super();
        moveHistory = new ArrayList<PlayerMove>(200);
		board = getBoard();
		window = new JFrame("Checkers");
		window.setContentPane(checkers);
		window.pack();
		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
		window.setLocation( (screensize.width - window.getWidth())/2,
				(screensize.height - window.getHeight())/2 );
		window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		window.setResizable(false);  
		window.setVisible(true);
	}
	
	public static void main(String[] args) throws InterruptedException {
		Game game = new Game();
		
		
        try {
			Player red = new RandomPlayer();
			Player black = new RandomPlayer();
			// black = new PlayAhead(3);

			while (board.getLegalMoves() != null) {
			    CheckersMove move = null;
				final int currentPlayer = board.getCurrentPlayer();
				if (currentPlayer == CheckersData.RED) {
					move = red.getMove(board);
				} else {
					move = black.getMove(board);
				}
				game.doMove(currentPlayer, move);
			    System.out.println(game.getBoard());
			    //Pause for n milliseconds (1000 is 1 second)
			    Thread.sleep(100);
			}
			for (PlayerMove m:game.getMoveHistory()){
			    System.out.println(m);
			}
			Thread.sleep(200);
	        game.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	private void close() {
		window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
	}

	private List<PlayerMove> getMoveHistory() {
		return this.moveHistory;
	}

	public void doMove(int currentPlayer, CheckersMove move) {
        board.doMakeMove(move);
        moveHistory.add(new PlayerMove(currentPlayer, move));
	}

	public Board getBoard() {
		Board board1;
		if (board == null) {
			checkers = new Checkers();
			board1 = new Board();
		} else {
			board1 = board;
		}
//		//   Add drop downs for player selection
//		Vector<Player> players = new Vector<Player>(10);
//		players.add(new Human());
//		players.add(new RandomPlayer());
//		players.add(new PlayAhead());
//		JComboBox<Player> player1Combo = new JComboBox<>(players);
//		ActionListener l = new ActionListener() {
//			
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				if (e.getSource() instanceof JComboBox<?>) {
//				JComboBox<?> cb = (JComboBox<?>)e.getSource();
//				String cbName = (String)cb.getSelectedItem();
//				System.out.println("combo selected was "+cbName);
//				}
//			}
//		};
//		player1Combo.addActionListener(l);
////		 {
////		    public void actionPerformed(ActionEvent e) {
//////		        JComboBox<Player> cb = (JComboBox<Player>)e.getSource();
//////		        String petName = (String)cb.getSelectedItem();
//////		        player1Combo.updateLabel(petName);
////		    }
////		};
//		JComboBox<Player> player2Combo = new JComboBox<>(players);
//		board1.add(player1Combo);
//		board1.add(player2Combo);
//
//		/* Set the position and size of each component by calling
//       its setBounds() method. */
//
//		JButton newGameButton = checkers.getNewGameButton();
//		JButton resignButton = checkers.getResignButton();
//		final int width=120,height=30,dheigth=15;
//		final int xPosition = 210;
//		int yPosition = height+dheigth;
//
//		newGameButton.setBounds(xPosition, yPosition, width, height);
//		yPosition += height+dheigth;
//		resignButton.setBounds(xPosition, yPosition, width, height);
//		yPosition += height+dheigth;
//		player1Combo.setBounds(xPosition, yPosition, width, height);
//		yPosition += height+dheigth;
//		player2Combo.setBounds(xPosition, yPosition, width, height);
		
		return board1;
	}
	
	class PlayerMove {
		final int player;
		final CheckersMove move;
		PlayerMove(int p, CheckersMove m) {
			player = p;
			move = new CheckersMove(m);
		}
		@Override
		public String toString() {
			String color = "red:   ";
			if (player == CheckersData.BLACK) {
				color = "black: ";
			}
			return color + move;
		}
	}
}
